package com.changgou.goods.controller;


import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.exception.CustomException;
import com.changgou.goods.pojo.Categories;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Spu;
import com.changgou.goods.pojo.Template;
import com.changgou.goods.service.CategoryService;
import com.changgou.goods.service.SpecService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SpecService specService;
    /***
     * 分类列表分页搜索
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size) throws Exception {
        Page<Category> pageList = categoryService.findAll(page,size);
        if(pageList.size()==0){
            throw new CustomException(StatusCode.MESSAGE_BACD,"您查询的数据暂无，请联系管理核对！！！");
        }
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }


    /***
     * 分类列表下级分页搜索
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/searchNext/{id}/{page}/{size}" )
    public Result findPage(@PathVariable int id,@PathVariable  int page, @PathVariable  int size) throws Exception {
        Page<Category> pageList = categoryService.findAllByParentId(id,page,size);
        if(pageList.size()==0){
            throw new CustomException(StatusCode.MESSAGE_BACD,"您查询的数据暂无，请联系管理核对！！！");
        }
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 规格参数(模板)列表分页查询
     */
    @GetMapping(value = "searchTemplate/{page}/{size}")
    public Result TemplatefindPage(@PathVariable  int page, @PathVariable  int size) throws Exception {
        Page<Template> pageList = categoryService.TemplatefindAll(page,size);
        if(pageList.size()==0){
            throw new CustomException(StatusCode.MESSAGE_BACD,"您查询的数据暂无，请联系管理核对！！！");
        }
        PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 根据id删除商品分类
     * @param id
     * @return
     * fxq
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        try {
            categoryService.deleteById(id);
            return new Result(true,StatusCode.OK,"删除分类成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"删除分类失败");
        }
    }
    @GetMapping("/categories")
    public Result findCategories(){
        try {
            List<Categories> categories = categoryService.findCategories();
            return new Result(true,StatusCode.OK,"三级分类查询成功",categories);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"三级分类查询失败");
        }
    }


    /**
     *上级分类下拉框查询
     *跟据传来的id查询他的上级
     */
    @GetMapping("/selection/{id}")
    public Result selectSuperior(@PathVariable("id") Integer id){
        try {
            categoryService.selectParentId(id);
            return new Result(true,StatusCode.OK,"查询上级成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"查询上级失败");
        }
        }


    /**
     * 添加下级
     */
    @PostMapping
    public Result addSubordinate(@RequestBody Category category){
        try {
            categoryService.add(category);
            return new Result(true,StatusCode.OK,"新增下级成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR,"新增下级失败");
        }
    }


}
